package haltica.com.mx.ape.web;

import flexjson.JSONSerializer;
import haltica.com.mx.ape.domain.Cliente;

import java.util.List;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RooWebScaffold(path = "clientes", formBackingObject = Cliente.class)
@RequestMapping("/clientes")
@Controller
public class ClienteController {

	/*
	@RequestMapping(params = { "find=ByNombreClienteOrNombreCortoCliente", "form" }, method = RequestMethod.GET)
    public String findClientesByNombreClienteOrNombreCortoClienteForm(Model uiModel) {
        return "clientes/findClientesByNombreClienteOrNombreCortoCliente";
    }
    
    @RequestMapping(params = "find=ByNombreClienteOrNombreCortoCliente", method = RequestMethod.GET)
	public String findClientesByNombreClienteOrNombreCortoCliente(
			@RequestParam("nombreCliente") String nombreCliente,
			@RequestParam("nombreCortoCliente") String nombreCortoCliente,
			Model uiModel) {
    	
        uiModel.addAttribute("clientes", Cliente.findClientesByNombreClienteOrNombreCortoCliente(nombreCliente, nombreCortoCliente).getResultList());
        
        return "clientes/list";
    }
    */
	
	@RequestMapping(value = "/addCliente", method = RequestMethod.POST)
	public ResponseEntity<String> addEstatus(Cliente cliente) {
		
		HttpHeaders headers;
		String json;
		GenericResponse gRes;
		ResponseEntity<String> response;
		
		headers = new HttpHeaders();
		headers.set("Content-Type", "application/json");
		headers.add("Content-Type", "application/text; charset=utf-8");
		
		
		cliente.persist();
		
		gRes = new GenericResponse(Boolean.TRUE, cliente);
		json = new JSONSerializer().exclude("*.class").serialize(gRes);
		
		response = new ResponseEntity<String>(json, headers, HttpStatus.OK);
		
		return response;
	}
	
	
	@RequestMapping(value = "/listClientes", method = RequestMethod.GET)
	public ResponseEntity<String> listClientes(){
		List<Cliente> listC;
		HttpHeaders headers;
		
		headers = new HttpHeaders();
		headers.set("Content-Type", "application/json");
		headers.add("Content-Type", "application/text; charset=utf-8");
		
		listC = Cliente.findAllClientes();
		
		GenericResponse gRes = new GenericResponse(Boolean.TRUE, listC);
		String json = new JSONSerializer().exclude("*.class").serialize(gRes);
		
		ResponseEntity<String> response = new ResponseEntity<String>(json, headers, HttpStatus.OK);
		return response;
		
	}
	
}
